// 102.二叉树的层序遍历
package LeetcodeHot100;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;


public class Solution102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null)
            return result;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            List<Integer> floor = new ArrayList<>();
            int size = deque.size();
            while (size-- != 0) {
                TreeNode poll = deque.poll();
                floor.add(poll.val);
                if (poll.left != null)
                    deque.offer(poll.left);
                if (poll.right != null)
                    deque.offer(poll.right);
            }
            result.add(floor);
        }
        return result;
    }
}
